package com.example.musiccommunity.UserPage;

import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.musiccommunity.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class UserHome extends AppCompatActivity {
    private ArrayList<Fragment> fragments = new ArrayList<>();
    List<String> tabStrings = new ArrayList<>();
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private Fragment_Home fragment_home;
    private Fragment_News fragment_news;
    private Fragment_Mine fragment_mine;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_home);

        fragment_home = new Fragment_Home();
        fragment_news = new Fragment_News();
        fragment_mine= new Fragment_Mine();

        viewPager = (ViewPager) findViewById(R.id.view_pager_user);
        tabLayout = (TabLayout) findViewById(R.id.tl_title_user);

        fragments.add(fragment_home);
        fragments.add(fragment_news);
        fragments.add(fragment_mine);

        tabStrings.add("首页");
        tabStrings.add("资讯");
        tabStrings.add("我的");
        if (viewPager != null) {
            viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int position) {
                    return fragments.get(position);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }

                @Override
                public void destroyItem(ViewGroup container, int position, Object object) {
                    super.destroyItem(container, position, object);
                }



                @Nullable
                @Override
                public CharSequence getPageTitle(int position) {
                    return tabStrings.get(position);
                }
            });
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    // 不需要在这里做什么
                }

                @Override
                public void onPageSelected(int position) {
                    Fragment selectedFragment = fragments.get(position);
//                    if (selectedFragment instanceof OnDataRefreshListener) {
//                        System.out.println("刷新");
//                        ((OnDataRefreshListener) selectedFragment).refreshData();
//                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                    // 不需要在这里做什么
                }
            });
        } else {
            throw new NullPointerException();
        }

        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(3);
    }
}